<?php
/**
 * Created by PhpStorm.
 * User: iceberg
 * Date: 2018-10-12
 * Time: 16:18
 */

namespace app\service;
use app\model\User;
use app\validate\UserValidate;
use think\facade\Request;

class UserService
{
    //分页
    public function page()
    {
        //获取request参数
        $request = Request::get();
        $username = isset($request['username']) ? $request['username'] : '';
        $status = isset($request['status']) ? $request['status'] : -1;
        //封装where查询条件
        $where = [];
        $status == -1   ||  $where[]   = ['status', '=' ,$status];
        empty($username)    ||  $where[]   = ['username', 'like', '%'.$username.'%'];
        //拼接page参数
        $pageParam = ['query' => []];
        $pageParam['query']['status'] = $status;
        $pageParam['query']['username'] = $username;
        return User::where($where)->order('id desc')->paginate(10, false, $pageParam);
    }
    //统计个数
    public function count()
    {
        return User::count();
    }

    //保存数据
    public function save()
    {
        Request::isPost() || die('request not post!');
        //获取参数
        $param = Request::param();
        //是否通过验证
        $error = $this->_validate($param, 'add');

        if(is_null($error)){
            //添加数据
            $user = new User();
            //密码加密
            $param['password'] = md5($param['password']);
            //检测错误
            //allowField(true)过虑post数组中非数据表字段数据
            if( $user->allowField(true)->save($param) ){
                return ajax_return(0, '保存成功');
            }else{
                return ajax_return(100, '保存失败');
            }
        }else{
            return ajax_return(100, $error);
        }
    }

    //根据id得到数据
    public function edit($id)
    {
        return User::get($id);
    }

    //更新
    public function update()
    {
        Request::isPost() || die('request not post!');
        //获取参数
        $param = Request::param();
        //是否通过验证
        $error = $this->_validate($param, 'edit');

        if(is_null($error)){
            $user = User::get($param['id']);
            $user->username     = $param['username'];
            $user->phone        = $param['phone'];
            $user->email        = $param['email'];
            $user->truename     = $param['truename'];
            $user->status       = $param['status'];
            $user->role         = $param['role'];

            //密码有改就更新
            if(!empty($param['password'])){
                $user->password = md5($param['password']);
            }
            // 检测错误
            if( $user->save() ){
                return ajax_return(0, '修改成功');
            }else{
                return ajax_return(100, '修改失败');
            }
        }else{
            return ajax_return(100,$error);
        }
    }

    //删除
    public function delete($id)
    {
        Request::isPost() || die('request not post!');

        if(User::destroy($id)){
            return ajax_return(0, '删除成功');
        }else{
            return ajax_return(100, '删除失败');
        }
    }

    //验证器
    private function _validate($data, $scene='')
    {
        //验证
        $validate = new UserValidate();
        if(!$validate->scene($scene)->check($data))
        {
            return $validate->getError();
        }
    }

}